using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Diagnostics;
using System.Xml;
using System.Xml.Schema; 
using XPathParser; 
using System.Xml.Linq; 
using System.Xml.XPath;

namespace CodeGenerationPipelineInterface
{
	[DebuggerDisplay("{ DebugInfo }")]
	public class Configuration : IXmlElement
	{
        private IXmlDocument document;
        private XNode node;
        private ManagedList<Location> locations;
        private ConfigurationData configurationData;
        private const int unbounded = int.MaxValue;

		public Configuration(IXmlDocument document)
		{
           locations = new ManagedList<Location>("Locations", "Configuration.Locations", 1, unbounded);
            
            this.document = document;
		}

        public IXmlDocument Document 
        { 
            get
            {
                return document;
            }

            set
            {
                document = value;
            }
        }

		[XPathAttribute("/ElementBuild/Requirements/RequiredConfiguration/Configuration/Location")]
		[MinOccursAttribute(1)]
		[MaxOccursAttribute(unbounded)]
 
		public ManagedList<Location> Locations
		{
			get
			{
				return locations;
			}
		}

		[XPathAttribute("/ElementBuild/Requirements/RequiredConfiguration/Configuration/ConfigurationData")]
		[MinOccursAttribute(1)]
		[MaxOccursAttribute(1)]
		public ConfigurationData ConfigurationData
		{
			get
			{
				return configurationData;
			}
		}

		public string ElementName
		{
			get
			{
				return "Configuration";
			}
		}

		public string DebugInfo
		{
			get
			{
				return "Configuration";
			}
		}

        public XNode Node
        {
            get
            {
                return node;
            }
        }

        public IEnumerable<string> CDataElements
        {
            get
            {
                var element = (XElement)node;

                return element.Nodes().Where(n => n is XCData).Cast<XCData>().Select(c => c.Value);
            }
        }

        public void Read(XNode node)
        {
            var elementNode = (XElement) node;
            var prefix = elementNode.GetPrefixOfNamespace(elementNode.Name.Namespace);

            // Location

            foreach (var elementLocation in node.XPathSelectElements(prefix + ":Location", this.Document.NamespaceManager))
            {
                var location = new Location(this.Document);

                location.Read(elementLocation);

                this.locations.Add(location);
            }

            // ConfigurationData

            var elementConfigurationData = node.XPathSelectElement(prefix + ":ConfigurationData", this.Document.NamespaceManager);
            var configurationData = new ConfigurationData(this.Document);

            configurationData.Read(elementConfigurationData);

            this.configurationData  = configurationData;

            this.node = node;
        }

        public void Write(XNode parentNode, XNamespace elementNamespace)
        {
            var parentElement = (XElement)parentNode;
            var element = new XElement(elementNamespace + this.ElementName);

            parentElement.Add(element);


            foreach (var locations in this.Locations)
            {
                locations.Write(element, elementNamespace);
            }

            var configurationData = this.ConfigurationData;

            configurationData.Write(element, elementNamespace);
        }

        public void Write(XDocument document, XmlNamespaceManager namespaces)
        {
            var namespaceAttributes = namespaces.Cast<string>().Where(s => !string.IsNullOrEmpty(s) && s != "xmlns" && s != "xml").Select(s => new XAttribute(XNamespace.Xmlns + s, XNamespace.Get(namespaces.LookupNamespace(s))));
            var element = new XElement(this.document.ElementNamespace + this.ElementName, namespaceAttributes.ToArray<object>());

            document.Add(element);

            foreach (var locations in this.Locations)
            {
                locations.Write(element, this.Document.ElementNamespace);
            }

            var configurationData = this.ConfigurationData;
            configurationData.Write(element, this.document.ElementNamespace);

        }

        public bool Validate(object sender, ValidationEventHandler handler)
        {
            var valid = true;
            foreach (Location locations in this.Locations)
            {
                locations.Validate(sender, (sender2, e) =>
                {
                    handler(sender2, e);
                    valid = false;
                });
			}

            ConfigurationData configurationData = this.ConfigurationData;
            configurationData.Validate(sender, (sender2, e) =>
            {
                handler(sender2, e);
                valid = false;
            });

            return valid;
        }
    }
}
